var basePath="http://localhost:8080/";

$(document).ready(function () {
     initUploadify();
});

//图片上传
var formatErrorMsg = "文件类型错误";
var fileOverMsg = "文件大小不能超过100M";
var fileMaxSize = 100*1024*1024; //100M

function initUploadify(){
   $(".file_upload").fileupload({
                url:basePath+ "file/upload",
                dataType: 'json',
                autoUpload: true,
                maxFileSize: fileMaxSize,
                acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            }).on('fileuploadadd', function (e, data) {
                //添加文件
                name = data.files[0].name;
            }).on('fileuploadprocessalways', function (e, data) {
                //文件校验
                if(data.files.error){
                    $(this).removeAttr("disabled");
                    if(data.files[0].error=="File type not allowed"){
                        alert(formatErrorMsg);
                    }
                    if(data.files[0].error=="File is too large"){
                        alert(fileOverMsg);
                    }
                }
            }).on('fileuploadsubmit', function (e, data) {
                //提交前
                //$("#attName").html(data.files[0].name);
                //data.formData = {'fileName': $("#attName").val(),'pathName':'case'};
            }).on('fileuploaddone', function (e, data) {
                //上传完成
                var code = data.result.code;
                var result = data.result.row;
                if(code == 0){
                	$(this).parents(".li_photo").find('input[type="hidden"]').val(result.id);
                	$(this).parents(".add_span").replaceWith('<img src="'+result.webPath+'" class="photo">')
                }else{
                    alert(data.result.msg);
                }
            });
}
//提交报修订单
function addFixOrder(){
	if(validate()==0)
		return false;
	var photoStr=getPhotoIds();
	$.ajax({
		url:basePath+'fixOrder/addOrder',
		method:'post',
		dataType:'json',
		data:{
			article:$.trim($('#article').val()),
		    description:$.trim($('#description').val()),
		    photoStr:photoStr,
		    accountId:$.trim($('#accountId').val()),
		    name:$.trim($('#name').val()),
		    phone:$.trim($('#phone').val()),
		    address:$.trim($('#address').val()),
		    remark:$.trim($('#remark').val()),
		},
		success:function(data){
			if(data.code==0){
				alert("提交成功")
				fixOrderListTopic();
			}else{
				alert("提交失败")
			}
		}
	});
}
//获取上传图片
function getPhotoIds(){
	var str="";
	$('.li_photo').each(function(){
		var id=$(this).find('input[type="hidden"]').val();
		if(id!=""&&id!=undefined){
		   str+=id;
		   str+=","
		}
	});
	str=str.substring(0,str.length-1);
	return str;
}
//校验
function validate(){
	var article=$.trim($('#article').val());
    var des=$.trim($('#description').val());
    var accountId=$.trim($('#accountId').val());
    var name=$.trim($('#name').val());
    var phone=$.trim($('#phone').val());
    var address=$.trim($('#address').val());
    if(article==""||article==undefined){
    	alert("报修物品不能为空");
    	return 0;
    }
    if(des==""||des==undefined){
    	alert("报修信息不能为空");
    	return 0;
    }
    if(accountId==""||accountId==undefined){
    	alert("账户信息过期");
    	return 0;
    }
    if(name==""||name==undefined){
    	alert("报修人不能为空");
    	return 0;
    }
    if(phone==""||phone==undefined){
    	alert("电话不能为空");
    	return 0;
    } 
    if(address==""||address==undefined){
    	alert("地址不能为空");
    	return 0;
    }
    return 1;
}
